package com.github.data_manage

import com.github.data_manage.domain.po.FundDepthResearchAttach
import com.github.data_manage.domain.po.FundDepthResearchAttach.Companion.propertyNameMap
import org.apache.poi.xssf.usermodel.XSSFWorkbook
import org.junit.jupiter.api.Test
import org.springframework.util.StringUtils
import java.math.BigDecimal
import java.nio.charset.StandardCharsets
import java.nio.file.Paths
import java.nio.file.StandardOpenOption
import kotlin.io.path.Path
import kotlin.io.path.bufferedReader
import kotlin.io.path.bufferedWriter
import kotlin.io.path.inputStream
import kotlin.reflect.full.memberProperties

/**
 *
 * @author chenhainan
 * @since 2025/8/27
 */
class ExcelHeaderTests {

    @Test
    fun excelHeaderTest() {
//        val fundDepthResearchAttach = FundDepthResearchAttach()
//        for (entry in FundDepthResearchAttach.propertyMap) {
//            val name = entry.key
//            val property = entry.value
//            val actualValue =
//                // 尝试类型转换
//                when (property.returnType.classifier) {
//                    String::class -> ""
//                    BigDecimal::class -> BigDecimal(1)
//                    else -> null
//                }
//            property.setter.call(fundDepthResearchAttach,actualValue)
//        }
//        println(fundDepthResearchAttach)
//        Paths.get("/Users/chenhainan/IdeaProjects/data-manage/src/main/resources/static/深度调研导入模板.xlsx")
//            .inputStream(
//                StandardOpenOption.READ
//            ).use { fis ->
////                val writer = Paths.get("/Users/chenhainan/Downloads/FundDepthResearchAttach2.kt")
////                    .bufferedWriter(StandardCharsets.UTF_8)
//                val workbook = XSSFWorkbook(fis)
//
//                val dataSheet = workbook.getSheetAt(0)
//                val firstRow = dataSheet.getRow(0)
//                val thirdRow = dataSheet.getRow(2)
//                val indexNameMap = HashMap<Int, String>(400)
//                var lastHeadFirstRowCellValue: String? = ""
//                for (index in 1..thirdRow.lastCellNum) {
//                    val firstRowCellValue =
//                        firstRow.getCell(index)?.stringCellValue?.trim()?.replace("\n", "") ?: ""
//                    if (StringUtils.hasText(firstRowCellValue)) {
//                        lastHeadFirstRowCellValue = firstRowCellValue
//                    }
//                    val thirdRowCellValue =
//                        thirdRow.getCell(index)?.stringCellValue?.trim()?.replace("\n", "") ?: ""
//                    if (!StringUtils.hasText(thirdRowCellValue)) {
//                        if (!StringUtils.hasText(lastHeadFirstRowCellValue)) {
//                            return@use
//                        }
//                        continue
//                    }
//                    indexNameMap[index] = if (StringUtils.hasText(thirdRowCellValue)) {
//                        "$lastHeadFirstRowCellValue$thirdRowCellValue"
//                    } else thirdRowCellValue
//                }
//                println(indexNameMap)
//
//                for (name in indexNameMap.values) {
//                    val fieldName = propertyNameMap[name]
//                    if (fieldName != null) {
//                        println(fieldName)
//                    } else {
//                        println("name $name not found")
//                    }
//                }


//                var lastFirstRowCellValue: String? = ""
//                Paths.get("/Users/chenhainan/Downloads/FundDepthResearchAttach.kt")
//                    .bufferedReader(StandardCharsets.UTF_8).use { reader ->
//                        reader.readLines()
//                    }.map { line ->
//                        return@map if (line.contains("@ExcelProperty") && !line.contains("value")) {
//                            val indexString = line.substring(line.indexOf('=') + 1, line.indexOf(')')).trim()
//                            val index = indexString.toInt()
//
//                            val firstRowCellValue =
//                                firstRow.getCell(index)?.stringCellValue?.trim()?.replace("\n", "")
//                            if (StringUtils.hasText(firstRowCellValue)) {
//                                lastFirstRowCellValue = firstRowCellValue
//                            }
//                            val thirdRowCellValue =
//                                thirdRow.getCell(index)?.stringCellValue?.trim()?.replace("\n", "")
//
//                            val valueString = if (StringUtils.hasText(thirdRowCellValue)) {
//                                ",value = [\"$lastFirstRowCellValue$thirdRowCellValue\"])"
//                            } else ",value = [\"$lastFirstRowCellValue\"])"
//
//                            StringBuilder(line.replace(")", ""))
//                                .append(valueString).toString()
//
//                        } else line
//                    }.forEach {
//                        writer.write(it);
//                        writer.newLine()
//                        writer.flush()
//                    }
//                writer.close()
//            }

    }

    @Test
    fun replaceMethodTest() {
        Paths.get("/Users/chenhainan/IdeaProjects/data-manage/src/main/kotlin/com/github/data_manage/domain/po/FundDepthResearchAttach.kt")
            .bufferedReader(StandardCharsets.UTF_8)
            .use { reader ->
                var matched = false
                reader.forEachLine { line ->
                    if (matched) {
                        if (StringUtils.hasText(line)) {
                            val realLine = line.replace("open var ", "")
                                .replace(": BigDecimal? = BigDecimal.ZERO", "")
                                .replace(" ", "")
                            val newLine = """
                                cumulativeInfo.${realLine} = 
                                    cumulativeCalc(lastDepthResearchAttach?.cumulativeInfo?.${realLine},depthResearchAttach.performance${
                                firstUpperCase(
                                    realLine.replace(
                                        "Cumulative",
                                        ""
                                    )
                                )
                            })
                            """.trimIndent()
                            println(newLine)
                        }
                    } else {
                        matched = line.contains("peruCumulative")
                    }
                }
            }
    }

    fun firstUpperCase(s: String): String {
        return "${s[0].uppercase()}${s.substring(1)}"
    }
}

